#!/bin/sh

revision="setvariant v0.2.2 (2023-07-02 by MIB-Wiki)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
fi
echo -ne "$ME-$thisname---->\n" | $TEE -i -a $LOG

if [ -f $TMP/reboot.mib ]; then
	echo -ne "Aborted because the reboot is running...\n" | $TEE -a $LOG
	return 2> /dev/null
fi

MIBCAP=3
. $thisdir/../config/MIBCHECK

if [ "$2" = "-noboot" ]; then
	noboot=$2
else
	noboot=""
fi

readvar ()
{
echo -ne "Reading Variant_2 from EEPROM...\n" | $TEE -i -a $LOG
echo -ne "Reading Unit Class from EEPROM...\n" | $TEE -i -a $LOG
VARTYPE=$($E2P r DE 1 | $SED -rn 's/^0x\S+\W+(.*?)$/\1/p') 2>> $LOG #Unit class: 01=High 04=Premium
#echo $VARTYPE
echo -ne "Reading Feature bits from EEPROM...\n" | $TEE -i -a $LOG
feature_byte=$($E2P r DF 1 | $SED -rn 's/^0x\S+\W+(.*?)$/\1/p') 2>> $LOG
binary_value=$(echo "ibase=16; obase=2; $feature_byte" | $BC ) 2>> $LOG
binary_value=$(echo "00000000"${binary_value} | tail -c9) 2>> $LOG
#echo $binary_value
#feature bits
	VARTEL="$(echo -ne $binary_value | cut -c8)" #Telephone
	VARNAV="$(echo -ne $binary_value | cut -c7)" #Navigation
	VARDAB="$(echo -ne $binary_value | cut -c6)" #DAB
	VARSIR="$(echo -ne $binary_value | cut -c5)" #Sirius
	VARLTE="$(echo -ne $binary_value | cut -c4)" #LTE
	VAR2DNAV="$(echo -ne $binary_value | cut -c3)" #2d Nav
	VARMMIRADIO="$(echo -ne $binary_value | cut -c2)" #MMI Radio
	#echo 0$VARMMIRADIO$VAR2DNAV$VARLTE$VARSIR$VARDAB$VARNAV$VARTEL
echo -ne "Reading Region from EEPROM...\n" | $TEE -i -a $LOG
VARREG=$($E2P r E0 1 | $SED -rn 's/^0x\S+\W+(.*?)$/\1/p') 2>> $LOG #Region: 02=EU
#echo $VARREG

#error handler for variant_1
VARERROR=0
}

convertvar2hex ()
{
if [ $VARTYPE = 01 ]; then #High
	VARTYPE="48 "
elif [ $VARTYPE = 04 ]; then #Premium
	VARTYPE="50 "
else
	echo -ne "Type unknown\n" | $TEE -i -a $LOG
	VARTYPE=""
	VARERROR=1
fi

#if [ $VARTEL = 01 ]; then #ENABLED
#	VARTEL="54 "
#elif [ $VARTEL = 00 ]; then #OFF
#	VARTEL=""
#else
#	echo -ne "Telephone status unknown\n" | $TEE -i -a $LOG
#	VARTEL=""
#	VARERROR=1
#fi

if [ $VARNAV = 1 ]; then #ENABLED
	VARNAV="4e "
elif [ $VARNAV = 0 ]; then #OFF
	VARNAV=""
else
	echo -ne "Navigation status unknown\n" | $TEE -i -a $LOG
	VARNAV=""
	VARERROR=1
fi

if [ $VARDAB = 1 ]; then #ENABLED
	VARDAB="44 "
elif [ $VARDAB = 0 ]; then #OFF
	VARDAB=""
else
	echo -ne "DAB status unknown\n" | $TEE -i -a $LOG
	VARDAB=""
	VARERROR=1
fi

if [ $VARSIR = 1 ]; then #ENABLED
	VARSIR="53 "
elif [ $VARSIR = 0 ]; then #OFF
	VARSIR=""
else
	echo -ne "Sirius status unknown\n" | $TEE -i -a $LOG
	VARSIR=""
	VARERROR=1
fi

if [ $VARLTE = 1 ]; then #ENABLED
	VARLTE="4c "
elif [ $VARLTE = 0 ]; then #OFF
	VARLTE=""
else
	echo -ne "LTE status unknown\n" | $TEE -i -a $LOG
	VARLTE=""
	VARERROR=1
fi

case $VARREG in
	01*) VARREG="45 52 " ;; #ER
	02*) VARREG="45 55 " ;; #EU
	03*) VARREG="55 53 " ;; #US
	04*) VARREG="52 57 " ;; #RW
	05*) VARREG="43 4e " ;; #CN
	06*) VARREG="4a 50 " ;; #JP
	07*) VARREG="4B 52 " ;; #KR
	08*) VARREG=""		  #Asia?
		echo -ne "Region code 8 unknown\n" | $TEE -i -a $LOG
		VARERROR=1 ;;
	09*) VARREG="54 57 " ;; #TW
	*) VARREG=""
		echo -ne "Region unknown\n" | $TEE -i -a $LOG
		VARERROR=1 ;;
esac
}

IdentBlock ()
{

#find IdentBlock for unit

if [[ "$TRAINVERSION" = *VW* ]] || [[ "$TRAINVERSION" = *SK* ]]; then
	if [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 1 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 2 ]]; then
		#High2_EU_Nav_DAB
		IDENT="30 32 30" #020
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 1 ]] && [[ "$VARTEL" = 1 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 2 ]]; then
		#High2_EU_Nav_DAB_Phone
		IDENT="30 32 31" #021
	elif [[ "$VARLTE" = 1 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 1 ]] && [[ "$VARTEL" = 1 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 2 ]]; then
		#High2_EU_Nav_DAB_PhoneLTE
		IDENT="30 32 31" #021
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 1 ]] && [[ "$VARREG" = 3 ]]; then
		#High2_NAR_EX_Nav_Sirius
		IDENT="30 32 36" #026
	elif [[ "$VARLTE" = 1 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 1 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 2 ]]; then
		#High2_EU_Nav_PhoneLTE
		IDENT="30 34 32" #042
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 1 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 2 ]]; then
		#High2_EU_Nav
		IDENT="30 34 33" #043
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 1 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 2 ]]; then
		#High2_EU_Nav_Phone
		IDENT="30 34 34" #044
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 4 ]]; then
		#High2_RoW_Nav
		IDENT="30 34 35" #045
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 1 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 4 ]]; then
		#High2_RoW_Nav_Phone
		IDENT="30 34 36" #046
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 1 ]] && [[ "$VARREG" = 3 ]]; then
		#High2_NAR_Nav_Sirius
		IDENT="30 34 37" #047
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 5 ]]; then
		#High2_CN_Nav
		IDENT="30 34 38" #048
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 6 ]]; then
		#High2_JP_Nav
		IDENT="30 35 30" #050
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 8 ]]; then
		#High2_TW_Nav
		IDENT="30 33 32" #032
	else
		echo -ne "no matching IdentBlock found for this variant\n"
	fi
elif [[ "$TRAINVERSION" = *SE* ]]; then
	IDENT="30 32 30" #020
#elif [[ "$TRAINVERSION" = *AU* ]]; then
	#TODO: Add Audi, Bentley
elif [[ "$TRAINVERSION" = *POG* ]]; then
	if [[ "$VARLTE" = 1 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 1 ]] && [[ "$VARTEL" = 1 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 2 ]]; then
		#High2_EU_Nav_DAB_PhoneLTE
		IDENT="30 33 35" #035
	elif [[ "$VARLTE" = 1 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 1 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 2 ]]; then
		#High2_EU_Nav_PhoneLTE
		IDENT="30 33 36" #036
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 1 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 2 ]]; then
		#High2_EU_Nav_DAB
		IDENT="30 33 37" #037
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 2 ]]; then
		#High2_EU_Nav
		IDENT="30 33 37" #038
	elif [[ "$VARLTE" = 1 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 1 ]] && [[ "$VARSIR" = 1 ]] && [[ "$VARREG" = 3 ]]; then
		#High2_US_Nav_PhoneLTE_Sirius
		IDENT="38 38 30" #880
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 1 ]] && [[ "$VARREG" = 3 ]]; then
		#High2_US_Nav_Sirius
		IDENT="38 39 31" #891
	elif [[ "$VARLTE" = 1 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 1 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 5 ]]; then
		#High2_CN_Nav_PhoneLTE
		IDENT="38 39 34" #894
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 5 ]]; then
		#High2_CN_Nav
		IDENT="38 39 35" #895
	elif [[ "$VARLTE" = 0 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 0 ]] && [[ "$VARSIR" = 0 ]] && [[ "$VARREG" = 7 ]]; then
		#High2_KR_Nav
		IDENT="38 39 39" #899
	else
		echo -ne "no matching IdentBlock found for this variant\n"
	fi
elif [[ "$TRAINVERSION" = *BY* ]]; then
	if [[ "$VARLTE" = 1 ]] && [[ "$VARNAV" = 1 ]] && [[ "$VARDAB" = 0 ]] && [[ "$VARTEL" = 1 ]] && [[ "$VARSIR" = 1 ]] && [[ "$VARREG" = 3 ]]; then
		#High2_US_Nav_PhoneLTE_Sirius
		IDENT="38 39 30" #890
	else
		echo -ne "no matching IdentBlock found for this variant\n"
	fi
else
	echo -ne "no matching IdentBlock found for this variant\n"
fi

}

case $1 in
-var) {
trap '' 2

echo -ne "---- Read original Variant_1 from unit\n" | $TEE -i -a $LOG
COMPONENT="$($E2P r BA D | $SED -rn 's/^0x\S+\W+(.*?)$/\1/p' | $SED -rn 's:\W*(\S\S)\W*:0x\1\n:pg' | $SED -rn '/^0x/p' | $XXD -r -p | $SED 's/[^a-zA-Z0-9_-]//g' )" 2>> $LOG
echo -ne "---- Variant_1: $COMPONENT\n" | $TEE -i -a $LOG
echo -ne "---- Read original Variant_2 from unit\n" | $TEE -i -a $LOG
VARIANT2="$($SED -n 's/Variant = '\''\(.*\)'\''/\1/p' /net/rcc/dev/shmem/version.txt | $SED 's/[^a-zA-Z0-9._-]//g')" 2>> $LOG
echo -ne "---- Variant_2: $VARIANT2\n" | $TEE -i -a $LOG

readvar
convertvar2hex


if [ $VARERROR = 0 ]; then
	echo -ne "---- Writing 13 spaces to Variant_1\n" | $TEE -i -a $LOG
	$E2P w BA 20 20 20 20 20 20 20 20 20 20 20 20 20 >> $LOG
	#Build variant 1 string
	#TNDL in Variant_2 is LND in Variant_1 - no T and order is different
	EVAR="4d 55 2d "$VARTYPE"2d "$VARLTE$VARNAV$VARDAB$VARSIR"2d "$VARREG
	AVAR=$(echo "$EVAR" | $XXD -r -p)
	echo -ne "---- New Variant_1: "$AVAR"\n" | $TEE -i -a $LOG
	echo -ne "---- Writing generated Variant_1 into EEPROM\n" | $TEE -i -a $LOG
	$E2P w BA $EVAR >> $LOG
	COMPONENT="$($E2P r BA D | $SED -rn 's/^0x\S+\W+(.*?)$/\1/p' | $SED -rn 's:\W*(\S\S)\W*:0x\1\n:pg' | $SED -rn '/^0x/p' | $XXD -r -p | $SED 's/[^a-zA-Z0-9_-]//g' )" 2>> $LOG
	echo -ne "New Variant_1 in EEPROM: $COMPONENT\n" | $TEE -i -a $LOG
else
	echo -ne "Error occured during creation of Variant_1\n" | $TEE -i -a $LOG
	echo -ne "Check messages above\n"
	echo -ne "No changes to Variant_1 will be applied\n\n" | $TEE -i -a $LOG
	exit 1
fi
echo -ne "\n$thisname done!\n" | $TEE -i -a $LOG

trap 2
[ -z "$noboot" ] && . $thisdir/reboot -t 10
[ -z "$noboot" ] && return 2> /dev/null

};;

-identblock) {

trap '' 2

echo -ne "IdentBlock will be derived from Variant\n" | $TEE -i -a $LOG

readvar
IdentBlock

if [ ! -z $IDENT ]; then
	echo -ne "write IdentBlock $IDENT to eeprom\n" | $TEE -i -a $LOG
	$E2P w 86 $IDENT | $TEE -i -a $LOG
	$E2P w 91 $IDENT | $TEE -i -a $LOG
else
	echo -ne "IdentBlock not found\n" | $TEE -i -a $LOG
fi

trap 2

[ -z "$noboot" ] && . $thisdir/reboot -t 10
[ -z "$noboot" ] && return 2> /dev/null

};;

	# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION] [-noboot]"
		echo ""
		echo "Options:"
		echo "        -var			set variant_1"
		echo "        -identblock		set IdentBlock based on variant_2"
		echo "        --help			show this help"
		echo ""
		echo "Note: Backup will only work in RCC bash!"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""
	};;
esac

exit 0
